if (trigger_get("shop") == true)
{
    inventory_selected = -1;
    if (shop_item_transaction == false)
    {
        if (keyboard_check_pressed(global.dpad[1]) == true)
        {
            if (shop_item_selected == ds_map_size(global.shop_items) - 1)
            {
                shop_item_selected = 0;
            }
            else
            {
                shop_item_selected += 1;
            }
        }
        if (keyboard_check_pressed(global.dpad[0]) == true)
        {
            if (shop_item_selected == 0)
            {
                shop_item_selected = ds_map_size(global.shop_items) - 1;
            }
            else
            {
                shop_item_selected -= 1;
            }
        }
        if (keyboard_check_pressed(global.button[0]) == true) xor (keyboard_check_pressed(global.button[1]) == true)
        {
            if ((variable_get("money") > shop_item_transaction_price) && (inventory_item_cancarry(ds_list_find_value(global.shop_list, shop_item_selected), 1) == true))
            {
                shop_item_transaction_index = ds_list_find_value(global.shop_list, shop_item_selected);
                shop_item_transaction_price = ds_map_find_value(global.shop_items, shop_item_transaction_index);
                shop_item_transaction = true;
            }
        }
        if (keyboard_check_pressed(global.button[2]) == true)
        {
            trigger_set("shop",false);
            inventory_selected = 0;
            shop_item_selected = 0;
            shop_item_transaction_index = 0;
            shop_item_transaction_price = 0;
            shop_item_transaction_quantity = 1;
            ds_map_clear(global.shop_items);
            ds_list_clear(global.shop_list);
            dialog_nextline = true;
        }
    }
    else
    {
        if (keyboard_check_pressed(global.dpad[1]) == true)
        {
            if (shop_item_transaction_selected == 1)
            {
                shop_item_transaction_selected = 0;
            }
            else
            {
                shop_item_transaction_selected += 1;
            }
        }
        if (keyboard_check_pressed(global.dpad[0]) == true)
        {
            if (shop_item_transaction_selected == 0)
            {
                shop_item_transaction_selected = 1;
            }
            else
            {
                shop_item_transaction_selected -= 1;
            }
        }
        if (keyboard_check_pressed(global.dpad[2]) == true)
        {
            if (shop_item_transaction_quantity > 1)
            {
                shop_item_transaction_quantity -= 1;
            }
            else
            {
                shop_item_transaction_quantity = 99;
            }
        }
        if (keyboard_check_pressed(global.dpad[3]) == true)
        {
            if (shop_item_transaction_quantity < 99)
            {
                shop_item_transaction_quantity += 1;
            }
            else
            {
                shop_item_transaction_quantity = 1;
            }
        }
        if (keyboard_check_pressed(global.button[0]) == true) xor (keyboard_check_pressed(global.button[1]) == true)
        {
            switch (shop_item_transaction_selected)
            {
                case 0:
                if ((variable_get("money") >= (shop_item_transaction_price*shop_item_transaction_quantity)) && (inventory_item_cancarry(shop_item_transaction_index,shop_item_transaction_quantity) == true))
                {
                    variable_add("money",-(shop_item_transaction_price * shop_item_transaction_quantity));
                    inventory_item_give(shop_item_transaction_index, shop_item_transaction_quantity);
                    shop_item_transaction_quantity = 1;
                    shop_item_transaction_selected = 0;
                    shop_item_transaction = false;
                }
                break;
                case 1:
                    shop_item_transaction_quantity = 1;
                    shop_item_transaction_selected = 0;
                    shop_item_transaction = false;
                break;
            }
        }
        if (keyboard_check_pressed(global.button[2]) == true)
        {
            shop_item_transaction = false;
            shop_item_transaction_index = 0;
            shop_item_transaction_price = 0;
            shop_item_transaction_quantity = 1;
            shop_item_transaction_selected = 0;
        }
    }
}
